/**  
 * Project Name:api-webhook  
 * File Name:EmailClient.java  
 * Package Name:com.example.service.webhook.api  
 * Date:2016年7月19日下午5:17:42  
 * Copyright (c) 2016, yuandong@51dojo.com All Rights Reserved.  
 *  
*/  
  
package com.example.service.webhook.business.email.api;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.example.service.webhook.api.MoxieClient;
import com.example.service.webhook.business.email.dto.EmailBillInstallmentList;
import com.example.service.webhook.business.email.dto.EmailBillList;
import com.example.service.webhook.business.email.dto.EmailBillShoppingSheetList;

import retrofit2.Call;
import retrofit2.Response;

/**  
 * ClassName:EmailClient <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2016年7月19日 下午5:17:42 <br/>  
 * @author   yuandong  
 * @version    
 * @since    JDK 1.6  
 * @see        
 */
@Component
public class EmailClient extends MoxieClient{
	
	 private EmailApi api;
	 
	 @Autowired
	public EmailClient(@Value("${moxie.api.baseUrl}") String apiBaseUrl,
            @Value("${moxie.api.token}") String apiToken) {
		super(apiBaseUrl, apiToken);  
		 api = retrofit.create(EmailApi.class);
	}

	private static final Logger LOGGER = LoggerFactory.getLogger(EmailClient.class);
	 
	
	public EmailBillList getBillsByEmailId(String emailId) throws IOException {
        Call<EmailBillList> call = api.getBillsByEmailId(emailId);
        Response<EmailBillList> response = call.execute();
        if (response.code() == 200) {
            return response.body();
        } else {
            LOGGER.info("getBillsByEmailId, status:{}, message:{}", response.code(), response.message());
        }
        return null;
    }


    public EmailBillShoppingSheetList getBillShoppingSheets(String billId,int page,int size) throws IOException {
        Call<EmailBillShoppingSheetList> call = api.getBillShoppingSheets(billId,page,size);
        Response<EmailBillShoppingSheetList> response = call.execute();
        if (response.code() == 200) {
            return response.body();
        } else {
            LOGGER.info("getBillShoppingSheets, status:{}, message:{}", response.code(), response.message());
        }
        return null;
    }
    
    
    public EmailBillInstallmentList getBillInstallments(String billId,int page,int size) throws IOException {
        Call<EmailBillInstallmentList> call = api.getBillInstallments(billId, page, size);
        Response<EmailBillInstallmentList> response = call.execute();
        if (response.code() == 200) {
            return response.body();
        } else {
            LOGGER.info("getBillInstallments, status:{}, message:{}", response.code(), response.message());
        }
        return null;
    }


	
}
  
